package database.viewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import database.table.DBTableData;

public class TablePanel extends JPanel implements ListSelectionListener {
	private JTable				table;
	private JScrollPane			scroll;
	private List<DBTableData>	dataList;
	private MessageTableModel	modelMsg;
	
	private ActionListener		control;
	
	public TablePanel( ActionListener control ) {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
		this.control = control;
		
		modelMsg = new MessageTableModel("No table selected");
		table = new JTable(modelMsg);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.getSelectionModel().addListSelectionListener(this);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scroll = new JScrollPane(table);
		dataList = new ArrayList<DBTableData>();
		
		add( scroll, BorderLayout.CENTER );
		
	}
	
	public int addTableData( DBTableData data ) {
		dataList.add(data);
		if (dataList.size() == 1)
			table.setModel(data);
		
		return dataList.size() - 1;
	}
	
	public int getTableDataIndex( String name ) {
		for (int row = 0; row < dataList.size(); row++)
			if (dataList.get(row).getName().equals(name))
				return row;
		
		return -1;
	}
	
	public DBTableData getSelectedTableData() {
		if (table.getModel() != modelMsg )
			return (DBTableData) table.getModel();
		
		return null;
	}
	
	public int getSelectedRow() {
		return table.getSelectedRow();
	}
	
	public boolean selectTableData( int i ) {
		try {
			DBTableData data = dataList.get(i);
			table.setModel(data);
			return true;
		}
		catch (IndexOutOfBoundsException e) {
			clearTableData("Table not loaded");
			return false;
		}
	}
	
	public boolean selectTableData( String name ) {
		return selectTableData( getTableDataIndex(name) );
	}
	
	public void clearTableData( String msg ) {
		modelMsg.msg = msg;
		if (table.getModel() != modelMsg)
			table.setModel(modelMsg);
		else
			modelMsg.fireTableStructureChanged();
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		ActionEvent event = new ActionEvent(this, 0, DBTableViewer.COMMAND_ELEMENTCHANGE);
		control.actionPerformed(event);
	}
	
	private static class MessageTableModel extends AbstractTableModel {
		String msg;
		
		public MessageTableModel( String msg ) {
			this.msg = msg;
		}
		
		@Override
		public int getColumnCount() {
			return 1;
		}

		@Override
		public int getRowCount() {
			return 0;
		}
		
		@Override
		public String getColumnName(int column) {
			return msg;
		}
		
		@Override
		public Object getValueAt(int row, int col) {
			return null;
		}
		
	}
}
